package gxprod.androidai;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class PopupAIActivity extends Activity implements OnInitListener, android.content.DialogInterface.OnClickListener, OnDismissListener {

	private String say;
	private TextToSpeech mTts;
	private AlertDialog alertDialog;
	private boolean TTSReady;
	
	public void onCreate(Bundle state) {
		super.onCreate(state);

		Intent i = getIntent();

		say = i.getStringExtra("say");

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		AlertDialog alertDialog = builder.create();
		
		alertDialog.setOnDismissListener(this);
		
		alertDialog.setButton("Ok", this);
		alertDialog.setButton2("Open AndroidAI", this);
		
		alertDialog.setTitle("Reminder");
		alertDialog.setMessage(i.getStringExtra("note"));

		alertDialog.show();

		mTts = new TextToSpeech(this, this);
	}

	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = mTts.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) 
				Log.e("", "Language is not available.");
			else 
				TTSReady = true;
			if(TTSReady)
				mTts.speak(say, TextToSpeech.QUEUE_FLUSH, null);
			say = null;
		}
	}

	@Override
	public void onDestroy() {
		if (mTts != null) {
			while(mTts.isSpeaking()) 
				Thread.yield();
			mTts.stop();
			mTts.shutdown();
		}
		super.onDestroy();
	}

	@Override
	public void onClick(DialogInterface dialog, int id) {
		if(alertDialog != null)
			alertDialog.dismiss();
		else
			finish();
		if(id == R.id.button2) 
			startActivity(new Intent(this, AIActivity.class));
	}

	@Override
	public void onDismiss(DialogInterface arg0) {
		finish();
	}	
}

/*
public class PopupAIActivity extends AIActivity implements OnClickListener {

	private String say;

	public void onCreate(Bundle state) {
		if(state == null)
			state = new Bundle();

		state.putBoolean("isPopup", true);
		super.onCreate(state);

		createGUI();
		super.initializeSpeech();

		Intent sender = getIntent();		
		say = sender.getStringExtra("say");
	}

	private void createGUI() {

		boolean dialogMode = false;
		dialogMode = super.getCallingPackage() != null;
		if(dialogMode)
			dialogMode = super.getCallingPackage().equals("gxprod.androidai");

		Intent sender = getIntent();
		super.setContentView(R.layout.main_popup);

		if(dialogMode) {
			super.setTheme(android.R.style.Theme_Dialog);
		}

		((Button)findViewById(R.id.button1)).setOnClickListener(this);
		((Button)findViewById(R.id.button2)).setOnClickListener(this);

		((TextView)findViewById(R.id.textView1)).setText(sender.getStringExtra("title"));
		((TextView)findViewById(R.id.textView2)).setText(sender.getStringExtra("note"));
	}

	public void onInit(int status) {
		super.onInit(status);
		if(TTSReady)
			super.mTts.speak(say, TextToSpeech.QUEUE_FLUSH, null);
		say = null;
	}

	public void onClick(View v) {

		switch(v.getId()) {

			case R.id.button1:		// ok
				getIntent().putExtra("isPopup", false);
				finish();
				break;

			case R.id.button2:		// open snooze
				super.startActivity(new Intent(this, AIActivity.class));
				finish();
				break;
		}
	}
}
 */